﻿#include "mainwindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),workerThread(nullptr)
{
    // 创建界面元素
    startButton = new QPushButton("Start Thread", this);
    stopButton = new QPushButton("Stop Thread", this);
    stopButton->setEnabled(false);  // 初始化时禁用停止按钮
    statusLabel = new QLabel("Status: Waiting", this);
    progressBar = new QProgressBar(this);
    progressBar->setRange(0, 100);
    resultField = new QLineEdit(this);
    resultField->setReadOnly(true);

    // 布局设置
    QVBoxLayout *layout = new QVBoxLayout;
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addWidget(startButton);
    buttonLayout->addWidget(stopButton);

    layout->addLayout(buttonLayout);
    layout->addWidget(progressBar);
    layout->addWidget(resultField);
    layout->addWidget(statusLabel);

    QWidget *centralWidget = new QWidget(this);
    centralWidget->setLayout(layout);
    setCentralWidget(centralWidget);

    // 连接按钮点击信号到槽函数
    connect(startButton, &QPushButton::clicked, this, &MainWindow::startThread);
    connect(stopButton, &QPushButton::clicked, this, &MainWindow::stopThread);
}

MainWindow::~MainWindow()
{
    if (workerThread) {
        workerThread->requestStop();
        workerThread->quit();
        workerThread->wait();
        delete workerThread;
    }
}

void MainWindow::startThread()
{
    if (workerThread) return;

    workerThread = new WorkerThread(this);

    // 连接线程的信号到主窗口的槽函数
    connect(workerThread, &WorkerThread::progressUpdate, this, &MainWindow::updateProgress);
    connect(workerThread, &WorkerThread::resultReady, this, &MainWindow::handleResults);
    connect(workerThread, &QThread::finished, workerThread, &QObject::deleteLater);

    // 更新UI状态
    startButton->setEnabled(false);
    stopButton->setEnabled(true);
    statusLabel->setText("Status: Running");
    progressBar->setValue(0);

    // 启动线程
    workerThread->start();
}

void MainWindow::stopThread()
{
    if (workerThread) {
        workerThread->requestStop();
        stopButton->setEnabled(false);  // 停止按钮不可用
    }
}

void MainWindow::updateProgress(int percentage)
{
    progressBar->setValue(percentage);
}

void MainWindow::handleResults(const QString &result)
{
    statusLabel->setText("Status: Finished");
    resultField->setText(result);
    startButton->setEnabled(true);
    stopButton->setEnabled(false);
    workerThread = nullptr;  // 重置线程指针
}


void WorkerThread::run()
{
    int totalSteps = 100;  // 假设任务有100步
    QString result;

    for (int i = 0; i <= totalSteps; ++i) {
        {
            QMutexLocker locker(&mutex);
            if (shouldStop)
                break;
        }

        // 生成随机数并累加到结果字符串中
        int randomValue = QRandomGenerator::global()->bounded(100);
        result += QString::number(randomValue) + " ";

        // 发送进度更新信号
        emit progressUpdate(i);

        // 模拟耗时任务
        QThread::msleep(50);
    }

    emit resultReady(result);  // 任务完成时发送结果信号
}

void WorkerThread::requestStop()
{
    QMutexLocker locker(&mutex);
    shouldStop = true;
}
